package com.ycMc.service.impl;

import com.ycMc.bean.request.OrderDetailRequest;
import com.ycMc.bean.request.OrderRequest;
import com.ycMc.bean.response.ChartResponse;
import com.ycMc.common.bean.PageBean;
import com.ycMc.common.bean.Response;
import com.ycMc.constant.OrderStateConstant;
import com.ycMc.constant.UserConstant;
import com.ycMc.domain.*;
import com.ycMc.domain.result.ChartInfo;
import com.ycMc.mapper.*;
import com.ycMc.service.BaseService;
import com.ycMc.service.OrderService;
import com.ycMc.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by libo2 on 2017/8/23.
 */
@Service
public class OrderServiceImpl extends BaseService implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MoneyRecordMapper moneyRecordMapper;

    @Override
    public void delete(String orderId) {
        //如果订单编号为 待付款则返回库存
        Order order = orderMapper.selectOne(orderId);

        if(order.getOrderState()==OrderStateConstant.ORDER_STATE_NO_CONFIRM){
        List<OrderDetail> orderDetailList = orderDetailMapper.getOrderDetailByOrderId(orderId,0,0);
            for(OrderDetail orderDetail:orderDetailList){
                String productId = orderDetail.getProductId();
                int productNumber = orderDetail.getNumber();
                productMapper.updateSubtractionStore(productId, productNumber * -1);
                orderDetailMapper.delete(orderDetail.getOrderDetailId());
            }
        }
        orderMapper.delete(orderId);
    }

    @Override
    public void insert(Order order) {
        order.setId(0);
        order.setOrderId(BaseUtils.generateOrderId());
        order.setOrderState(OrderStateConstant.ORDER_STATE_NO_PAYMENT);
        order.setFareStatus(2);
        order.setPaymentStatus(2);
        orderMapper.insert(order);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=RuntimeException.class)
    public Response insertOrdersByProductIds(OrderDetailRequest request,User user) {
        Response response = new Response();

        String orderId= BaseUtils.generateOrderId();
        String[] productAndNumbers = request.getProductIdsAndNumbers().split("#");
        //根据productIds 获取数据
        double totalPrice = 0.0;
        int totalNumber = 0;
        for(String productAndNumber :productAndNumbers){
            String productId_temp = productAndNumber.split("@")[0];
            int number_temp =Integer.parseInt( productAndNumber.split("@")[1]);
            double productPrice_temp =Double.parseDouble(productAndNumber.split("@")[2]);
            Product product = productMapper.selectOne(productId_temp);
            //添加订单详情
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(0);
            orderDetail.setOrderDetailId(BaseUtils.generateOrderDetailId());
            orderDetail.setCreateTime(System.currentTimeMillis());
            orderDetail.setDeleted(2);
            orderDetail.setNumber(number_temp);
            orderDetail.setOrderId(orderId);
            orderDetail.setOrderState(OrderStateConstant.ORDER_STATE_NO_CONFIRM);
            orderDetail.setProductBrand(product.getProductBrand());
            orderDetail.setProductId(product.getProductId());
            orderDetail.setProductName(product.getProductName());
            orderDetail.setProductVersion(product.getProductVersion());
            orderDetail.setSinglePrice(productPrice_temp);
            totalPrice= totalPrice+productPrice_temp*number_temp;
            totalNumber=totalNumber+number_temp;
            if(user.getPermission()==UserConstant.ADMIN_LEVEL){
            orderDetail.setUserId(request.getUserId());}
            else if(user.getPermission()==UserConstant.USER_LEVEL){
                orderDetail.setUserId(request.getUserId());
            }
            if(product.getStore()<number_temp){
                System.out.println("");
                response.setStatusCode(Response.SUCCESS);
                response.setMsg("商品库存不足，下单失败");
                throw new RuntimeException("商品库存不足，下单失败");

            }else{
                productMapper.updateSubtractionStore(productId_temp, number_temp);}
            orderDetailMapper.insert(orderDetail);

        }

        Order order = new Order();
        //添加订单
        order.setId(0);
        order.setOrderId(orderId);
        order.setOrderState(OrderStateConstant.ORDER_STATE_NO_CONFIRM);
        order.setFareStatus(2);
        order.setPaymentStatus(2);
        order.setTaxStatus(2);
        order.setCreateTime(System.currentTimeMillis());
        order.setDeleted(2);
        order.setFareStatus(2);
        order.setNumber(totalNumber);
       order.setRealName(request.getRealName());
        order.setShoppingAddress(request.getShoppingAddress());
        order.setUserId(request.getUserId());
        order.setUserPhone(request.getUserPhone());
        order.setTotalPrice(totalPrice);
        order.setOrderRemark(request.getOrderRemark());
        orderMapper.insert(order);


//        //自动判断余额
//        User userTemp =userMapper.selectOne(request.getUserId());
//
//        if(userTemp.getUserMoney()>totalPrice){
//            //自动扣款
//            MoneyRecord moneyRecord = new MoneyRecord();
//
//            moneyRecord.setId(0);
//            moneyRecord.setCreateTime(System.currentTimeMillis());
//            moneyRecord.setMoney(totalPrice);
//            moneyRecord.setMoneyRecordId(BaseUtils.generateMoneyRecordId());
//            moneyRecord.setOperatorId("system");
//            moneyRecord.setOrderId(orderId);
//            moneyRecord.setPayId("库存系统自动扣款");
//            moneyRecord.setPayMethod("系统");
//            moneyRecord.setRemark("订单编号："+orderId);
//            moneyRecord.setTradeType("出账");
//            moneyRecord.setUserId(request.getUserId());
//            moneyRecordMapper.insert(moneyRecord);
//            //生成moneyRecord 的记录
//            //订单记录改变
//            order.setOrderId(orderId);
//            order.setOrderState(OrderStateConstant.ORDER_STATE_NO_CONFIRM);
//            orderMapper.update(order);
//            //用户扣款
//            User userTemp2 = new User();
//            userTemp2.setUserId(userTemp.getUserId());
//            userTemp2.setUserMoney(userTemp.getUserMoney());
//            userMapper.update(userTemp2);
//
//        }
//        else{
//            response.setStatusCode(Response.SUCCESS);
//            response.setMsg("用户余额不足，自动扣款失败，请添加余额");
//            return  response;
//        }

        response.setStatusCode(Response.SUCCESS);
        response.setMsg("下单成功");
        return  response;
    }

    @Override
    public List<Order> selectAll() {
        return orderMapper.select(null,null,0,null,null,0,0,0,0);
    }

    @Override
    public List<Order> selectByUserId(String userId) {
        return orderMapper.select(null,null,0,userId,null,0,0,0,0);
    }

    @Override
    public Order selectOne(String orderId) {
        return orderMapper.selectOne(orderId);
    }

    @Override
    public List<OrderDetail> selectOrderDetailByOrderId(String orderId) {
        return orderDetailMapper.getOrderDetailByOrderId(orderId,0,0);
    }

    @Override
    public void update(Order order) {

        Order orderTemp = orderMapper.selectOne(order.getOrderId());

        if(order.getOrderState()==OrderStateConstant.ORDER_STATE_NO_DELIVER||orderTemp.getOrderState()==OrderStateConstant.ORDER_STATE_NO_CONFIRM){
                    //自动判断余额
        User userTemp =userMapper.selectOne(orderTemp.getUserId());

//        if(userTemp.getUserMoney()>orderTemp.getTotalPrice()){
            //自动扣款
            MoneyRecord moneyRecord = new MoneyRecord();

            moneyRecord.setId(0);
            moneyRecord.setCreateTime(System.currentTimeMillis());
            moneyRecord.setMoney(orderTemp.getTotalPrice());
            moneyRecord.setMoneyRecordId(BaseUtils.generateMoneyRecordId());
            moneyRecord.setOperatorId("system");
            moneyRecord.setOrderId(orderTemp.getOrderId());
            moneyRecord.setPayId("库存系统自动扣款");
            moneyRecord.setPayMethod("系统");
            moneyRecord.setRemark("订单编号："+orderTemp.getOrderId());
            moneyRecord.setTradeType("出账");
            moneyRecord.setUserId(orderTemp.getUserId());
            moneyRecordMapper.insert(moneyRecord);
            //生成moneyRecord 的记录
            //订单记录改变
            //用户扣款
            User userTemp2 = new User();
            userTemp2.setUserId(userTemp.getUserId());
            userTemp2.setUserMoney(userTemp.getUserMoney()-moneyRecord.getMoney());
            try{
            userMapper.updateUserMoney(userTemp2);}
            catch (BadSqlGrammarException e){}
            order.setPayTime(System.currentTimeMillis());
            order.setPaymentStatus(1);
            orderDetailMapper.updateOrderStateByOrderId(order.getOrderState(),order.getOrderId());
            orderMapper.update(order);
//        }
//        else{
//           throw  new NullPointerException("确认订单失败，用户约不足");
//        }
        }
        else if(order.getOrderState()==OrderStateConstant.ORDER_STATE__FINISH){
            order.setFareTime(System.currentTimeMillis());
            orderDetailMapper.updateOrderStateByOrderId(order.getOrderState(), order.getOrderId());
            orderMapper.update(order);
        }
        else{
            //orderDetailMapper.updateOrderStateByOrderId(order.getOrderState(),order.getOrderId());
            orderMapper.update(order);
        }


    }

    @Override
    public List<Order> select(Order order) {
        return orderMapper.select(order.getOrderId(),order.getUserId(),order.getOrderState(),order.getUserPhone(),order.getRealName(),order.getFareStatus(),order.getTaxStatus(),0,0);
    }

    @Override
    public PageBean query(String userId,int currentPage,int pageSize,int orderState,int taxStatus,long startTime,long endTime,String orderId,String shoppingAddress,String userPhone,String realName) {
        final int _currentPage = PageBean.countCurrentPage(currentPage);
        final int offset = PageBean.countOffset(pageSize,currentPage);

        List<Order> orderList = orderMapper.query(orderId,userId,orderState,userPhone,realName,0,shoppingAddress,taxStatus,startTime,endTime,offset,pageSize);

        return createPageBean(_currentPage,orderList,pageSize);
    }


    @Override
    public ChartResponse getChartByProductBrand(User user) {
        ChartResponse response =new ChartResponse();

        if(user.getPermission()== UserConstant.ADMIN_LEVEL){

            List<HashMap> hashMapList = orderDetailMapper.getChartByProductBrand(null);

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("productBrand")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("productBrand")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice+"");
        }
        else if(user.getPermission()==UserConstant.USER_LEVEL){
            List<HashMap> hashMapList = orderDetailMapper.getChartByProductBrand(user.getUserId());

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("productBrand")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("productBrand")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice + "");
        }

        response.setStatusCode(Response.SUCCESS);
        response.setMsg("success");
        return response;
    }


    @Override
    public ChartResponse getChartByProductId(User user) {
        ChartResponse response =new ChartResponse();

        if(user.getPermission()== UserConstant.ADMIN_LEVEL){

            List<HashMap> hashMapList = orderDetailMapper.getChartByProductName(null);

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("productName")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("productName")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice+"");
        }
        else if(user.getPermission()==UserConstant.USER_LEVEL){
            List<HashMap> hashMapList = orderDetailMapper.getChartByProductName(user.getUserId());

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("productName")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("productName")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice + "");
        }

        response.setStatusCode(Response.SUCCESS);
        response.setMsg("success");
        return response;
    }

    @Override
    public ChartResponse getChartByDays(User user) {
        ChartResponse response =new ChartResponse();

        if(user.getPermission()== UserConstant.ADMIN_LEVEL){

            List<HashMap> hashMapList = orderMapper.getChartByTimeDays(null);

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("createTimes")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("createTimes")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice+"");
        }
        else if(user.getPermission()==UserConstant.USER_LEVEL){
            List<HashMap> hashMapList = orderMapper.getChartByTimeDays(user.getUserId());

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("createTimes")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("createTimes")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice + "");
        }

        response.setStatusCode(Response.SUCCESS);
        response.setMsg("success");
        return response;
    }

    @Override
    public ChartResponse getChartByYears(User user) {
        ChartResponse response =new ChartResponse();

        if(user.getPermission()== UserConstant.ADMIN_LEVEL){

            List<HashMap> hashMapList = orderMapper.getChartByTimeYears(null);

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("createTimes")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("createTimes")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice+"");
        }
        else if(user.getPermission()==UserConstant.USER_LEVEL){
            List<HashMap> hashMapList = orderMapper.getChartByTimeYears(user.getUserId());

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("createTimes")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("createTimes")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice + "");
        }

        response.setStatusCode(Response.SUCCESS);
        response.setMsg("success");
        return response;
    }

    @Override
    public ChartResponse getChartByMonths(User user) {
        ChartResponse response =new ChartResponse();

        if(user.getPermission()== UserConstant.ADMIN_LEVEL){

            List<HashMap> hashMapList = orderMapper.getChartByTimeMonths(null);

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("createTimes")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("createTimes")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice+"");
        }
        else if(user.getPermission()==UserConstant.USER_LEVEL){
            List<HashMap> hashMapList = orderMapper.getChartByTimeMonths(user.getUserId());

            double totalPrice = 0.0;
            int totalNumber = 0;
            List<ChartInfo> chartInfoListCount = new ArrayList<ChartInfo>();
            List<ChartInfo> chartInfoListPrice= new ArrayList<ChartInfo>();
            for(HashMap hashMap :hashMapList){
                totalPrice = totalPrice+(Double)hashMap.get("totalPrice");
                totalNumber=totalNumber+Integer.parseInt(hashMap.get("totalNumber")+"");
                ChartInfo chartInfoPrice = new ChartInfo();
                chartInfoPrice.setXParam(hashMap.get("createTimes")+"");
                chartInfoPrice.setYParam(hashMap.get("totalPrice") + "");
                chartInfoListPrice.add(chartInfoPrice);
                ChartInfo chartInfoCount= new ChartInfo();
                chartInfoCount.setXParam(hashMap.get("createTimes")+"");
                chartInfoCount.setYParam(hashMap.get("totalNumber") + "");
                chartInfoListCount.add(chartInfoCount);
            }
            response.setChartByCount(chartInfoListCount);
            response.setChartByPrice(chartInfoListPrice);
            response.setTotalCount(totalNumber+"");
            response.setTotalPrice(totalPrice + "");
        }

        response.setStatusCode(Response.SUCCESS);
        response.setMsg("success");
        return response;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor=RuntimeException.class)
    public void updateOrderDetail(OrderDetailRequest request) {

        //获取原来的订单中的userId
        Order order_temp = orderMapper.selectOne(request.getOrderId());
        String userId = order_temp.getUserId();
        //删除订单并回复详情
        if(order_temp.getOrderState()==OrderStateConstant.ORDER_STATE_NO_CONFIRM){
            List<OrderDetail> orderDetailList = orderDetailMapper.getOrderDetailByOrderId(request.getOrderId(),0,0);
            for(OrderDetail orderDetail:orderDetailList){
                String productId = orderDetail.getProductId();
                int productNumber = orderDetail.getNumber();
                productMapper.updateSubtractionStore(productId, productNumber * -1);
                orderDetailMapper.delete(orderDetail.getOrderDetailId());
            }
        }



        String[] productAndNumbers = request.getProductIdsAndNumbers().split("#");
        //根据productIds 获取数据
        double totalPrice = 0.0;
        int totalNumber = 0;
        for(String productAndNumber :productAndNumbers){
            String productId_temp = productAndNumber.split("@")[0];
            int number_temp =Integer.parseInt( productAndNumber.split("@")[1]);
            double productPrice_temp =Double.parseDouble(productAndNumber.split("@")[2]);
            Product product = productMapper.selectOne(productId_temp);
            //添加订单详情
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setId(0);
            orderDetail.setOrderDetailId(BaseUtils.generateOrderDetailId());
            orderDetail.setCreateTime(System.currentTimeMillis());
            orderDetail.setDeleted(2);
            orderDetail.setNumber(number_temp);
            orderDetail.setOrderId(request.getOrderId());
            orderDetail.setOrderState(OrderStateConstant.ORDER_STATE_NO_CONFIRM);
            orderDetail.setProductBrand(product.getProductBrand());
            orderDetail.setProductId(product.getProductId());
            orderDetail.setProductName(product.getProductName());
            orderDetail.setProductVersion(product.getProductVersion());
            orderDetail.setSinglePrice(productPrice_temp);
            totalPrice= totalPrice+productPrice_temp*number_temp;
            totalNumber=totalNumber+number_temp;
                orderDetail.setUserId(request.getUserId());

            if(product.getStore()<number_temp){
                throw new RuntimeException("商品库存不足，下单失败");
            }else{
                productMapper.updateSubtractionStore(productId_temp,number_temp);}
            orderDetailMapper.insert(orderDetail);

        }


        //添加订单
        Order order = new Order();
        order.setOrderId(order_temp.getOrderId());
        order.setId(order_temp.getId());
        order.setNumber(totalNumber);
        order.setRealName(request.getRealName());
        order.setShoppingAddress(request.getShoppingAddress());
        order.setUserId(order_temp.getUserId());
        order.setUserPhone(request.getUserPhone());
        order.setTotalPrice(totalPrice);
        order.setOrderRemark(request.getOrderRemark());
        orderMapper.update(order);


    }
}
